<?php

namespace app\common\model\banxi\student;

use app\common\model\common\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 学生额外信息
 */
class StudentSaySentence extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_student_say_sentence';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
    ];
    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];


    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------

    //更新生词本 $studyType=>1学习 2测试 3复习/抗遗忘复习
    public function saveStudentSentence($studentSaySentence)
    {
        $saveList = [];
        foreach ($studentSaySentence as $sentence) {
            $info = $this->where([
                'studentId' => $sentence['studentId'],
                'sayTeachSentenceId' => $sentence['sayTeachSentenceId'],
                'strangeType' => $sentence['strangeType']
            ])->withTrashed()->find();
            if (!$info) {
                if ($sentence['isCorrect']) {
                    continue;
                }
                $sentence['reviewNum'] = 0;
                $sentence['graspNum'] = 0;
                $sentence['lastGraspDate'] = date('Y-m-d H:i:s');
                $sentence['joinDate'] = date('Y-m-d H:i:s');
                $saveList[] = $sentence;
            } else {
                if ($info['del'] > 0) {
                    $info->restore();
                }
                $info['reviewNum'] = $info['reviewNum'] + 1;
                if ($sentence['isCorrect']) {
                    $info['graspNum'] = $info['graspNum'] + 1;
                } else {
                    $info['graspNum'] = 0;
                }
                $info['lastGraspDate'] = date('Y-m-d H:i:s');
                if ($info['graspNum'] >= 3) {
                    $info['graspNum'] = 0;
                    $info->delete();
                } else {
                    $info->save();
                }
            }
        }
        if ($saveList){
            $this->saveAll($saveList);
        }
        return true;
    }

    //设置状态
    public function updateStudentSentence($studentId, $sentence, $studyType)
    {
        $info = $this->where([
            'sayTeachSentenceId' => $sentence['sayTeachSentenceId'],
            'studentId' => $studentId,
            'strangeType' => $sentence['strangeType']
        ])->withTrashed()->find();
        if (!$info) {
            if ($sentence['isCorrect']) {
                return true;
            }
            $sentence['reviewNum'] = 0;
            $sentence['graspNum'] = 0;
            $sentence['lastGraspDate'] = date('Y-m-d H:i:s');
            $sentence['joinDate'] = date('Y-m-d H:i:s');
            $this->save($sentence);
        } else {
            if ($sentence['isCorrect'] && $info['reviewNum'] == 0 && $info['graspNum'] == 0 && $studyType == 1) {
                $info->delete();
                return true;
            }
            if ($info['del'] > 0) {
                $info->restore();
            }
            $info['reviewNum'] = $info['reviewNum'] + 1;
            if ($sentence['isCorrect']) {
                $info['graspNum'] = $info['graspNum'] + 1;
            } else {
                $info['graspNum'] = 0;
            }
            $info['lastGraspDate'] = date('Y-m-d H:i:s');
            if ($info['graspNum'] >= 3) {
                $info['graspNum'] = 0;
                $info->delete();
            } else {
                $info->save();
            }
        }
        return true;

    }
    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------


}